package thinkingJava.chapter14;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

class ProxyDynamicHandler implements InvocationHandler {
	private Object proxied;
	public ProxyDynamicHandler(Object proxied) {
		this.proxied = proxied;
	}

	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		System.out.println("*** proxy : " + proxy.getClass() + ", method : " + method + ", args : " + args );
		if (args != null) {
			for (Object o : args) {
				System.out.println("  " + o);
			}
		}
		return method.invoke(proxied, args);
	}
}

public class SimpleDynamicProxy {
	public static void consumer(Interface face) {
		face.doSomething();
		face.somethingElse("bonodo");
	}
	public static void main(String[] args) {
		RealObject real = new RealObject();
		consumer(real);
		Interface proxy = (Interface)Proxy.newProxyInstance(
				Interface.class.getClassLoader(), new Class[]{Interface.class},
				new ProxyDynamicHandler(real));
		consumer(proxy);
	}
}
